<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use think\facade\Cache;
class Index extends  Common{
    
    public function index(){
        return View('index/index',$this->admin_count());
    }
    
    // 私有办法获取总数信息
    private function admin_count() {
        $data = ['app'=>D('app')->count(),
            'member'=>D('member')->count(),
            'content'=>D('article')->count(),
            'member_list'=>$this->member_count(),
            'content_count'=>$this->content_count(),
            'comment'=>D('comment')->count()];
        return $data;
    }
    
    
	private function member_count(){ 
		//统计开始时间
		$starttime = strtotime(date('Y-m-d'))-10*24*3600;

		//统计结束时间
		$endtime = strtotime(date('Y-m-d'));

		$where = "regdate > $starttime";  //无需加结束条件，否则统计不到今日数据
		$data = D('member')->field("COUNT(*) AS num,FROM_UNIXTIME(regdate, '%Y-%m-%d') AS gap")->where($where)->group('gap')->select();
		$arr = array();
		foreach ($data as $val){
			$arr[$val['gap']] = intval($val['num']);
		}

		for($i=$starttime; $i<=$endtime; $i = $i+10*24*3600){
			$num = isset($arr[date('Y-m-d',$i)]) ? $arr[date('Y-m-d',$i)] : 0;				
			$result['day'][] = date('Y-m-d',$i);
			$result['num'][] = $num;
		}
		$result = json_encode($result);
		return $result;
	}
	
	
    
    private function content_count(){
        //统计开始时间
		$starttime = strtotime(date('Y-m-d'))-10*24*3600;

		//统计结束时间
		$endtime = strtotime(date('Y-m-d'));

		$where = "inputtime > $starttime";  //无需加结束条件，否则统计不到今日数据
		$data = D('comment')->field("COUNT(*) AS num,FROM_UNIXTIME(inputtime, '%Y-%m-%d') AS gap")->where($where)->group('gap')->select();
		$arr = array();
		foreach ($data as $val){
			$arr[$val['gap']] = intval($val['num']);
		}

		for($i=$starttime; $i<=$endtime; $i = $i+10*24*3600){
			$num = isset($arr[date('Y-m-d',$i)]) ? $arr[date('Y-m-d',$i)] : 0;				
			$result['day'][] = date('Y-m-d',$i);
			$result['num'][] = $num;
		}
		$result = json_encode($result);
		return $result;
    }
    
    
    // 退出登录
    public function laout() {
    	Session('adminid', null);
    	Session('adminname', null);
    	Session('roleid', null);
    	Session('rolename', null);
    	Jump::success('退出成功', url('admin/login/index'));
    }
    
    
    
    // 修改密码
    public function password() {
        if (request()->isPost()){
            // 获取请求变量
            $postField = 'password';
			$data = request()->only(explode(',',$postField),'post',null);
			if(Session('roleid') != 1)
    	    Jump::error('权限不足！');
			if(!$data['password'])Jump::error("密码不能为空!");
			
			D('admin')->where('adminid',Session('adminid'))->update(['password'=>password($data['password'])]);
			Jump::success('修改成功');
			
        }
        sleep(1);
    	return View('index/password');
    }
    
    
    
    // 清除缓存
    public function clear_chache() {
    	if (request()->isPost()) {
    		Cache::clear();
             // 批量删除缓存文件
    		$this->del_dir(app()->getRootPath().'/runtime/admin/temp/');
    		Jump::success("清除成功!");
    	}
    }
    
    
    private function del_dir($dir) {
    	if(!is_dir($dir)) {
    		Jump::error("文档不存在!");
    	} else {
    		foreach(scandir($dir) as $row) {
    			if($row == '.' || $row == '..') {
    				continue;
    			}
    			$path = $dir .'/'. $row;
    			if(filetype($path) == 'dir') {
    				$this->del_dir($path);
    			} else {
    				unlink($path);
    			}
    		}
    		rmdir($dir);
    	}
    }    

    
}
